#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<string.h>
#include<unistd.h>
#include<linux/stat.h>

#define FIFO "PUBFIFO"
#define FIFO1 "PRIFIFO1"
#define FIFO2 "PRIFIFO2"

int main(int argc,char *argv[])
{
	FILE *fp;
	FILE *fp1;
	FILE *fp2;
	char readbuf[100];
	char writebuf[100];
	char buf[100];
	int i;
	if(argc<=1)
	{
		printf("usage:%s<pathname>\n",argv[0]);
		exit(1);
	}

	if(atoi(argv[1]) == 1)
	{
		while(1)
		{
			memset(writebuf,'\0',100);
			if((fp = fopen(FIFO,"w")) == NULL)
       		{
               	printf("Open FIFO failed.\n");
                exit(1);
        	}
			writebuf[0] = '1';
			scanf("%s",buf);
            strcat(buf,"\n");
			strcat(writebuf,buf);
			if(fputs(writebuf,fp) == EOF)
			{
				printf("Write FIFO failed.\n");
				exit(1);
			}
			fclose(fp);
			memset(readbuf,'\0',100);
            if((fp1 = fopen(FIFO1,"r")) == NULL)
            {
            	printf("Open FIFO1 failed.\n");
                exit(1);
            }
            if(fgets(readbuf,sizeof(readbuf),fp1) != NULL)
            {
                printf("PRIFIFO1'S content%s\n",readbuf);
				fclose(fp1);
			}
		}
	}
	else if(atoi(argv[1]) == 2)
    {
        while(1)
        {
             memset(writebuf,'\0',100);
             if((fp = fopen(FIFO,"w")) == NULL)
             {
             	printf("Open FIFO failed.\n");
                exit(1);
             }
             writebuf[0] = '2';
             scanf("%s",buf);
             strcat(buf,"\n");
             strcat(writebuf,buf);
             if(fputs(writebuf,fp) == EOF)
             {
             	printf("Write FIFO failed.\n");
                exit(1);
             }
             fclose(fp);
             memset(readbuf,'\0',100);
             if((fp2 = fopen(FIFO2,"r")) == NULL)
             {
             	printf("Open FIFO2 failed.\n");
                exit(1);
             }
             if(fgets(readbuf,sizeof(readbuf),fp2) != NULL)
             {
                printf("PRIFIFO2'S content%s\n",readbuf);
                fclose(fp2);
             }
        }
    }
}
